<script lang="ts" setup>
import { defineProps, defineEmits } from 'vue'
import { PictureFilled, UploadFilled } from '@element-plus/icons-vue'

// 定义props
defineProps<{
  inputContent: string
  showEmoji: boolean
  showFileDialog: boolean
  isQuestion: boolean
  muteVisible: boolean
  muteDuration: number,
  connectionStatus: string
}>()

// 定义emits
const emit = defineEmits<{
  (e: 'update:inputContent', value: string): void
  (e: 'update:showEmoji', value: boolean): void
  (e: 'update:showFileDialog', value: boolean): void
  (e: 'update:isQuestion', value: boolean): void
  (e: 'update:muteVisible', value: boolean): void
  (e: 'update:muteDuration', value: number): void
  (e: 'send'): void
  (e: 'selectEmoji', emoji: string): void
  (e: 'fileChange', event: Event): void
}>()

// 表情列表
const emojiList = ['😀', '😃', '😄', '😁', '😆', '😅', '😂', '🤣', '😇', '😉']

// 处理表情选择
const handleEmojiSelect = (emoji: string) => {
    emit('selectEmoji', emoji)
    emit('update:showEmoji', false)
}

// 处理文件选择
const handleFileSelect = (event: Event) => {
    const input = event.target as HTMLInputElement
    if (input.files?.length) {
        emit('fileChange', event)
    }
    // 通过emit更新props，不直接修改
    emit('update:showFileDialog', false)
}
</script>

<template>
  <div class="send-area">
    <div class="send-tools">
      <div class="left-tools">
        <span :class="['status', connectionStatus]">{{ {
          'connecting': '连接中',
          'connected': '已连接',
          'disconnected': '已断开'
        }[connectionStatus] }}</span>
        <!-- <el-button
          type="text"
          @click="emit('update:showEmoji', true)">
          <el-icon><PictureFilled /></el-icon>
        </el-button>
        <el-button
          type="text"
          @click="emit('update:showFileDialog', true)">
          <el-icon><UploadFilled /></el-icon>
        </el-button> -->
      </div>
      <el-dropdown>
        <el-button type="text">设置</el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>敬请期待</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <el-input
      :model-value="inputContent"
      @input="val => emit('update:inputContent', val)"
      type="textarea"
      :rows="3"
      placeholder="请输入讨论内容" />
    <div class="send-footer">
      <div class="footer-left">
        <!-- <el-checkbox
          :model-value="isQuestion"
          @change="val => emit('update:isQuestion', val)">
          提问
        </el-checkbox> -->
      </div>
      <div class="footer-right">
        <el-button
          type="primary"
          @click="emit('send')"
        >发送</el-button
        >
      </div>
    </div>
  </div>

  <!-- 禁言对话框 -->
  <el-dialog
    :model-value="muteVisible"
    @update:modelValue="emit('update:muteVisible', $event)"
    title="禁言时长">
    <el-input
      :model-value="muteDuration"
      type="number"
      @input="val => emit('update:muteDuration', Number(val))" />
    <template #footer>
      <el-button @click="emit('update:muteVisible', false)">取消</el-button>
      <el-button
        type="primary"
        @click="emit('update:muteVisible', false)"
        >确认</el-button
      >
    </template>
  </el-dialog>

  <!-- 表情选择弹窗 -->
  <el-dialog
    :model-value="showEmoji"
    @update:modelValue="emit('update:showEmoji', $event)"
    title="选择表情"
    width="300px">
    <div class="emoji-grid">
      <span
        v-for="(emoji, index) in emojiList"
        :key="index"
        class="emoji-item"
        @click="handleEmojiSelect(emoji)">
        {{ emoji }}
      </span>
    </div>
  </el-dialog>

  <!-- 文件上传弹窗 -->
  <el-dialog
    :model-value="showFileDialog"
    @update:modelValue="emit('update:showFileDialog', $event)"
    title="上传文件"
    width="400px">
    <div class="file-uploader">
      <input
        type="file"
        hidden
        ref="fileInput"
        @change="handleFileSelect" />
      <el-button
        type="primary"
        @click="() => ($refs.fileInput as HTMLInputElement)?.click()">
        <el-icon><UploadFilled /></el-icon> 选择文件
      </el-button>
      <p class="tip">支持上传任意类型文件</p>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  max-height: 300px;
  overflow-y: auto;

  .emoji-item {
    font-size: 24px;
    cursor: pointer;
    text-align: center;
    padding: 5px;
    transition: all 0.3s;

    &:hover {
      transform: scale(1.2);
      background: #f5f5f5;
    }
  }
}

.file-uploader {
  text-align: center;
  padding: 20px;

  .tip {
    color: #999;
    margin-top: 10px;
    font-size: 12px;
  }
}

.send-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left-tools {
    display: flex;
    gap: 10px;
  }
}

.send-area {
  border-top: 1px solid #eee;
  padding: 10px;

  .send-tools {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
  }

  .send-footer {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
  }
}
.status {
  &::before {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 5px;
  }
  &.connected::before {
    background-color: green;
  }
  &.disconnected::before {
    background-color: gray;
  }
}
</style>
